package com.action.log;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.log.LogFavorite;
import com.business.log.LogFavorBus;
import com.config.SystemGlobals;
import com.util.MD5;

public class AddFavor extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);

		String logNo = request.getParameter("logno");
		String favName = request.getParameter("favorname");
		String favInfo = request.getParameter("favorinfo");
		String username = request.getParameter("username");
		LogFavorite lf = new LogFavorite();
		lf.setFavInfo(favInfo);
		lf.setFavName(favName);
		lf.setLogNo(logNo);
		lf.setUsername(username);
		LogFavorBus lfb = new LogFavorBus();
		String action = request.getParameter("action");
		if (action.equals("add")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String favAddTime = sdf.format(new Date());
			String favNo = new MD5().getMD5ofStr(favName + "@" + username);
			lf.setFavNo(favNo);
			lf.setFavAddTime(favAddTime);
			lfb.add(lf);
			out
					.print("<script>alert(\"您成功地增加了日志收藏信息\");window.location=\"log/temp/"
							+ logNo + ".html\";</script>");
			out.close();
		}
		if (action.equals("update")) {
			String favorNo = request.getParameter("no");
			lfb.update(lf, favorNo);
			RequestDispatcher rd = request
					.getRequestDispatcher("user/favorlist.jsp");
			rd.forward(request, response);

		}
	}
}
